using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Configuration.Assemblies;
using System.Web;
using MeetingRoom.Data;
using MeetingRoom.Domain;

namespace MeetingRoom.Data.Implementation
{
    /// <summary>
    /// Summary description for SimpleScheduleQuery
    /// </summary>
    public class SimpleScheduleDataQuery
        : AbstractDataAccessObject,
        IGetSchedulesByMeetingRoomType,
        IGetSchedules
    {
        public System.Collections.Generic.List<MeetingRoom.Domain.Schedule>
            GetSchedulesByMeetingRoomType(MeetingRoomType meetingRoomType)
        {
            List<Schedule> schedules = null;
            string query =
                "select * from t_schedule where meetingroomtype_id = @MeetingRoomTypeId and status = 0";

            SqlCommand command = new SqlCommand(query, (SqlConnection)this.connection);
            SqlParameter meetingRoomTypeIdParam
                = new SqlParameter("@MeetingRoomTypeId", meetingRoomType.Id);
            command.Parameters.Add(meetingRoomTypeIdParam);

            if (this.connection.State != ConnectionState.Open)
            {
                this.connection.Open();
            }

            SqlDataReader dataReader = null;
            try
            {
                dataReader = command.ExecuteReader();
                schedules = ScheduleDAOSupport.Load(dataReader);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (dataReader != null)
                {
                    dataReader.Close();
                }
                command.Dispose();
            }

            return schedules;
        }
        
        public List<Schedule> GetSchedules()
        {
            List<Schedule> schedules = null;
            string query = "select * from t_schedule";

            SqlCommand command = new SqlCommand(query, (SqlConnection)this.connection);

            SqlDataReader dataReader = null;
            
            try{
                dataReader = command.ExecuteReader();
                schedules = ScheduleDAOSupport.Load(dataReader);
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally{
                dataReader.Close();
            }

            command.Dispose();

            return schedules;
        }

        public List<Schedule> GetSchedules(int minutes)
        {
            return this.GetSchedules(DateTime.Now, minutes);
        }

        public List<Schedule> GetSchedules(DateTime start, int minutes)
        {
            List<Schedule> list = new List<Schedule>();
            string query = "select * " +
                            "from t_schedule " +
                            "where status = 0 and " + 
                            "(datediff(mi, @Start, begin_time) between 0 and @Minutes);";

            SqlParameter parameterStart = new SqlParameter("@Start", start);
            SqlParameter parameter = new SqlParameter("@Minutes", minutes);

            SqlCommand command = new SqlCommand(query, (SqlConnection)this.connection);
            command.Parameters.Add(parameterStart);
            command.Parameters.Add(parameter);

            SqlDataReader dataReader = null;
            try
            {
                dataReader = command.ExecuteReader();
                list = ScheduleDAOSupport.Load(dataReader);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (dataReader != null && !dataReader.IsClosed)
                {
                    dataReader.Close();
                }
                command.Dispose();
            }

            return list;
        }
    }
}